package com.vdf.sisconsolid.controller;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.vdf.sisconsolid.persistencemodule.dao.TracingDao;
import com.vdf.sisconsolid.persistencemodule.dto.TracingDTO;
import com.vdf.sisconsolid.rest.requestresponsedata.ReqRespData;
import com.vdf.sisconsolid.rest.requestresponsedata.RequestAndRespondHandler;
import com.vdf.sisconsolid.rest.requestresponsedata.ValidatedField;
import com.vdf.sisconsolid.validator.TracingValidator;

public class TracingController {

	private static Logger log = Logger.getLogger(TracingController.class);
	
	public static Map<String, Object> processGetById(Long id) {
		log.info("Into processGetById, id: " + id);		
		TracingDTO tracingDTO = new TracingDao().find(id);

		return RequestAndRespondHandler.getResponseRest(true,
				ReqRespData.data.toString(), "Response Tracing", tracingDTO);
	}

	public static Map<String, Object> processPostOrPut(TracingDTO tracingDTO) {
		log.info("Into processPostOrPut, idTracingType: " + tracingDTO.getIdTracingType());
		log.info("idTracinge: " + tracingDTO.getIdTracing());
		List<ValidatedField> validations = TracingValidator.valid(tracingDTO);
		boolean success = validations.size() == 0;
		if (success) {
			new TracingDao().merge(tracingDTO);
			log.info("merge OK");
		} else {
			log.info("Problems with validations");
		}
		String message = success ? "Succsessful Operation"
				: "errorSavingTracing";
		return RequestAndRespondHandler.getResponseRest(
				validations.size() == 0, ReqRespData.validation.toString(),
				message, validations);
	}

	public static Map<String, Object> processDelete(Long id) {
		log.info("Into processDelete, id: " + id);
		new TracingDao().remove(id);
		log.info("remove OK");
		return RequestAndRespondHandler.getResponseRest(true,
				ReqRespData.info.toString(), "Registro eliminado.");
	}

}
